package com.zhiyou.app.shop.website.action;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.util.DateUtil;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.StockPool;
import com.zhiyou.app.shop.website.dao.pojo.StockType;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.dao.pojo.UserStock;
import com.zhiyou.app.shop.website.dao.pojo.UserStockInfo;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IPlayService;
import com.zhiyou.app.shop.website.service.IStockService;
import com.zhiyou.app.shop.website.service.IUserAccountService;
import com.zhiyou.app.shop.website.service.IUserStockInfoService;
import com.zhiyou.app.shop.website.service.IUserStockService;

public class StockManagerAction extends BaseAction {


	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IDicItemService dicItemService;
	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private IUserStockInfoService userStockInfoService;
	@Autowired
	private IStockService stockService;
	@Autowired
	private IPlayService playService;
	@Autowired
	private IUserStockService userStockService;

	private UserAccount userAccount;
	private Integer listingQuantity; //挂牌数量
	private UserStockInfo userStockInfo;  //最新一条股票交易记录
	private BigDecimal toSale; //可卖股票(元)
	private Integer listedQua; //挂牌数量
	private float price; //股票价格
	private Integer toBuy; //可买股票数量（估）
	private List<UserStockInfo> saleList;
	private List<UserStockInfo> buyList;
	private BigDecimal usetStockTotal;  //当前用户股票总价值
	//private UserStockInfo userStockInfo;  //最新一条股票交易记录
	private StockPool stockPool;
	
	private String stocknum;
	private String type; // 0:买    1:卖
	
	private Long totalStock; //距离下次涨价数量
	
	
	public String youxiPage(){
		String result = "start";  //复盘
		try {
			userAccount = userAccountService.findUserAccount(getCurrentUser().getUserAccount());
			//挂牌数量
			//listedQua = userStockInfoService.getListedQua(getCurrentUser().getUserId());
			price = stockService.findPrice();
			
			Long userStockA = userStockService.findUserStock(getCurrentUser().getUserAccount(), "1");//旧股
			Long userStockB = userStockService.findUserStock(getCurrentUser().getUserAccount(), "2");//新股
			userAccount.setStockNum(userStockA.intValue());
			userAccount.setStockSaleNum(userStockB.intValue());
			
			usetStockTotal = userStockService.findUserStockValue(getCurrentUser().getUserAccount());
			/*if(userStock != null){
				userAccount.setStockSaleNum(userStock.intValue());
			}else{
				userAccount.setStockSaleNum(0);
			}*/
			//可买入股票数量
			//toBuy = userAccount.getStockMoney().divide(new BigDecimal(price),2 ,BigDecimal.ROUND_HALF_EVEN).intValue();
			//股票总量
			//stockPool = stockService.getStockPool();
			
			//每天可买股票金额
			/* List<DicItem> stockSaleList = dicItemService.findByType(ShopConstants.STOCK_SALE_RATE);
			 DicItem stockSaleRate = null;
			 if(stockSaleList != null && stockSaleList.size() > 0){
				 stockSaleRate = stockSaleList.get(0);
			 }*/
			 pageSize = 10;
			 userStockService.findUserStock(getInitedPage());
			 StockType stock = stockService.findStockType();
			 if(stock != null){
				 totalStock = stock.getTotal();				 
			 }
			 
			 //只有股票总金额的值超过保级， 才允许卖出 每天卖出股票的2%
			 
			 //每天总共可以卖出股金的 2%  可以调整   在数据字典中配置
			 /*DicItem level = dicItemService.findByTypeAndCode(ShopConstants.LEVEL, getCurrentUser().getLevel());
			 if(new BigDecimal(userAccount.getStockNum()).multiply(new BigDecimal(price)).compareTo(new BigDecimal(level.getAttr1())) ==1 ){  //大于保级  可以卖出股票
				 BigDecimal saleDay = new BigDecimal(userAccount.getStockNum()).multiply(new BigDecimal(stockSaleRate.getCode()));
				 toSale = userStockInfoService.getAviSaleStock(getCurrentUser().getUserId(), saleDay, DateUtil.convertDateToString(new Date()));
			 }else{
				 toSale = BigDecimal.ZERO;
			 }*/
			//每天可买股票金额 
			// userStockInfo = stockService.getLastedStockTrade();
			 
			List<DicItem> stockFlagList = dicItemService.findByType(ShopConstants.STOCK_FLAG);
			DicItem stockFlag = new DicItem();
			if(stockFlagList != null && stockFlagList.size() >0){
				stockFlag = stockFlagList.get(0);
			}
			if("1".equals(stockFlag.getCode())){ //买  复盘
				result = "start";
			}else if("0".equals(stockFlag.getCode())){ //不买  停盘
				result = "stop";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
		 return result;
	}
	
	public String stockTrade(){
		Integer sotck = new Integer(stocknum);
		playService.doStockTrade(getCurrentUser().getUserId(), sotck, new BigDecimal(price), type);
		return SUCCESS;
	}
	
	public String stockList(){
		pageSize = 4;
		UserStockInfo userStockInfo = new UserStockInfo();
		userStockInfo.setType(type);
		stockService.findStockInfo(getInitedPage(), userStockInfo);
		return SUCCESS;
	}
	
	public String stockListmore(){
		UserStockInfo userStockInfo = new UserStockInfo();
		userStockInfo.setUserId(getCurrentUser().getUserAccount());
		userStockInfo.setType(type);
		userAccount = userAccountService.findUserAccount(getCurrentUser().getUserAccount());
		stockService.findStockInfo(getInitedPage(), userStockInfo);
		return SUCCESS;
	}
	
	/***********后台股票交易begin*******************************************************************/
	private List<Map<String,Object>>  resultList;//查询结果
	
	public List<Map<String, Object>> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = resultList;
	}
	/**
	 * 公司买卖
	 */
	public String compTradeList(){
		try{
			//type 0：公司买入 1：公司沽出
			int totalCount=userStockInfoService.findStockInfoCount(type, ShopConstants.MEMBER_COMPANY);
			Page page=getInitedPage();
			page.setTotalCount(totalCount);
			resultList=userStockInfoService.findStockInfo(type, ShopConstants.MEMBER_COMPANY, page);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 自由交易
	 * @return
	 */
	public String userTradeList(){
		try {
			// type 0：会员买入 1：会员沽出
			int totalCount = userStockInfoService.findStockInfoCount(type,ShopConstants.MEMBER);
			Page page = getInitedPage();
			page.setTotalCount(totalCount);
			resultList = userStockInfoService.findStockInfo(type,ShopConstants.MEMBER, page);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return SUCCESS;
	}
	
	/***********后台股票交易end*******************************************************************/

	public UserAccount getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(UserAccount userAccount) {
		this.userAccount = userAccount;
	}

	public Integer getListingQuantity() {
		return listingQuantity;
	}

	public void setListingQuantity(Integer listingQuantity) {
		this.listingQuantity = listingQuantity;
	}

	public UserStockInfo getUserStockInfo() {
		return userStockInfo;
	}

	public void setUserStockInfo(UserStockInfo userStockInfo) {
		this.userStockInfo = userStockInfo;
	}

	public BigDecimal getToSale() {
		return toSale;
	}

	public void setToSale(BigDecimal toSale) {
		this.toSale = toSale;
	}

	public Integer getListedQua() {
		return listedQua;
	}

	public void setListedQua(Integer listedQua) {
		this.listedQua = listedQua;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public Integer getToBuy() {
		return toBuy;
	}

	public void setToBuy(Integer toBuy) {
		this.toBuy = toBuy;
	}

	public StockPool getStockPool() {
		return stockPool;
	}

	public void setStockPool(StockPool stockPool) {
		this.stockPool = stockPool;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStocknum() {
		return stocknum;
	}

	public void setStocknum(String stocknum) {
		this.stocknum = stocknum;
	}

	public Long getTotalStock() {
		return totalStock;
	}

	public void setTotalStock(Long totalStock) {
		this.totalStock = totalStock;
	}

	public BigDecimal getUsetStockTotal() {
		return usetStockTotal;
	}

	public void setUsetStockTotal(BigDecimal usetStockTotal) {
		this.usetStockTotal = usetStockTotal;
	}
	
}
